package com.data.modules.mqt;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.Person;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.data.modules.gd.entity.GdProgramme;
import com.data.modules.jhl.service.ISmJhlSleepService;
import com.data.modules.smzh.entity.Facedevice;
import com.data.modules.smzh.service.IFacedeviceService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class PushCallback implements MqttCallback {
    @Autowired
    private MqttConfiguration mqttConfiguration;

    @Autowired
    private IFacedeviceService facedeviceService;

    @Autowired
    private ISmJhlSleepService smJhlSleepService;

    @Override
    public void connectionLost(Throwable cause) {        // 连接丢失后，一般在这里面进行重连
        log.info("连接断开，正在重连");
        MqttPushClient mqttPushClient = mqttConfiguration.getMqttPushClient();
        if (null != mqttPushClient) {
            mqttPushClient.connect(mqttConfiguration.getHost(), mqttConfiguration.getClientid(), mqttConfiguration.getUsername(),
                    mqttConfiguration.getPassword(), mqttConfiguration.getTimeout(), mqttConfiguration.getKeepalive());
            log.info("已重连");
        }

    }

    /**
     * 发送消息，消息到达后处理方法
     * @param token
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {

    }

    /**
     * 订阅主题接收到消息处理方法
     * @param topic
     * @param message
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) {

        String payLoad = "";
        log.info("接收消息主题 : " + topic);
        payLoad = new String(message.getPayload());
        log.info("接收消息内容 : " + payLoad);

        try {
            if(ObjectUtil.contains(topic,"/disconnected")){

                JSONObject jsonObject = JSON.parseObject(payLoad);
                String clientid = jsonObject.getString("clientid");
                String serialNo = clientid.substring(2,clientid.length());

                log.info("客户端id : " + serialNo);
                facedeviceService.update(new Facedevice().setStatus(0), new UpdateWrapper<Facedevice>().lambda().eq(Facedevice::getSerialNo,serialNo));

            }

            if(ObjectUtil.contains(topic,"/connected")){

                JSONObject jsonObject = JSON.parseObject(payLoad);
                String clientid = jsonObject.getString("clientid");

                String serialNo = clientid.substring(2,clientid.length());
                log.info("客户端id : " + serialNo);
                facedeviceService.update(new Facedevice().setStatus(1), new UpdateWrapper<Facedevice>().lambda().eq(Facedevice::getSerialNo,serialNo));
            }

            //睡眠日报
            if(ObjectUtil.contains(topic,"dayReport/ED713/")){
                String mac = topic.replace("dayReport/ED713/","");

                JSONObject jsonObject = JSON.parseObject(payLoad);
                smJhlSleepService.saveSleepInfo(jsonObject,mac);

            }
        } catch (Exception e) {
            log.error("订阅主题异常：",e.getMessage(),e);
        }

    }

}
